package com.snailvr.tv.presenter;

import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.snail.readyvr.util.EventBus;
import com.snailvr.tv.Definition;
import com.snailvr.tv.contract.RoomContact;
import com.snailvr.tv.event.LoadFailureEvent;
import com.snailvr.tv.network.entity.JXListResponBean;
import com.snailvr.tv.network.entity.RoomInfoBean;
import com.snailvr.tv.network.entity.RoomListResponBean;
import com.snailvr.tv.provider.RoomInfoProvider;

/**
 * Created by Administrator on 2017/2/10.
 */

public class RoomPresenter implements RoomContact.IRoomPresenter {
    private RoomContact.IRoomView mRoomView = null;
    private RoomInfoProvider mRoomInfoProvider;

    public RoomPresenter() {
        mRoomInfoProvider = new RoomInfoProvider();
    }

    @Override
    public void registerView(RoomContact.IRoomView _View) {
        if (_View == null) {
            throw new NullPointerException("View to register must not be null.");
        }
        this.mRoomView = _View;
        EventBus.getDefault().register(this);
    }

    @Override
    public void unregisterView() {
        this.mRoomView = null;
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void loadRoomInfo(int room_id) {
        mRoomInfoProvider.getRoomInfo(room_id);

    }

    @Override
    public void loadJXList(int page, int pageSize) {
        mRoomInfoProvider.getJXList(page,pageSize);
    }

    @Override
    public void loadRoomList(int channel_id,  int page, int page_size,boolean isLoadMore, boolean needClear) {
        mRoomInfoProvider.getRoomList(channel_id,page,page_size);
    }

    @Subscribe(thread = EventThread.MAIN_THREAD,tags = {@Tag(Definition.BusTag.ROOM_INFO_TAG)})
    public void onRoomInfoEvent(RoomInfoBean event) {
        if (mRoomView!=null) {
            mRoomView.showRoomInfo(event);
        }
    }

    @Subscribe(thread = EventThread.MAIN_THREAD,tags = {@Tag(Definition.BusTag.ROOM_LIST_INFO_TAG)})
    public void onRoomListEvent(RoomListResponBean event) {
        if(mRoomView!=null) {
            mRoomView.showRoomList(event);
        }
    }

    @Subscribe(thread = EventThread.MAIN_THREAD, tags = {@Tag(Definition.BusTag.ROOM_LIST_INFO_TAG)})
    public void onFailureEvent(LoadFailureEvent event) {
        if(mRoomView!=null) {
            mRoomView.showLoadFailureMsg(event.code,event.errorMsg);
        }
    }

    @Subscribe(thread = EventThread.MAIN_THREAD,tags = {@Tag(Definition.BusTag.JX_TAG)})
    public void onJXListEvent(JXListResponBean event) {
        if(mRoomView!=null) {
            mRoomView.showJXList(event.getData());
        }
    }

}
